package org.hoyi.web.ctrls;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hoyi.DB.ctrl.Console;
import org.hoyi.DB.model.DataTable;
import org.hoyi.dispatchs.IWebService;
import org.hoyi.web.events.IExcuteKeyListener;
import org.hoyi.web.events.OnClickListener;

import net.sf.json.JSONArray;

/**
 * Author:ellen Email:ellen@kuaifish.com CreateDate:2015-05-20
 * ModifyDate:2015-06-20 hoyi entities @ hoyi.org 使用请在项目关于内标注hoyi版权，
 * hoyi版权归hoyi.org所有
 */
public class IHoyiCtrl extends IWebService {
	
	public IExcuteKeyListener ExecuteListener;
	private IHoyiCtrl ParentCtrl;
	public String HtmlContent = "";
	
	/**
	 * 用来标识用哪一个的hoyip.html来展示
	 * 可用于在一个页面设置多个样式时候使用，
	 */
	public String themehtmls = "";
	
	/**
	 * html的os类型，直接添加到html访问路径中.
	 */
	public String htmlostype = "";
	
	public String rendedHTMLContent = "";
	/**
	 * 基于DataTable的数据源.
	 */
	public DataTable TableSource;
	/**
	 * 基于Entity的数据源。
	 */
	public List<?> EntitySource;
	/**
	 * 基于MAP的数据源。
	 */
	public Map<?, ?> MapSource;
	/**
	 * 支持JSON格式的数据源。
	 */
	public JSONArray JsonSource;
	
	public int DataCount = 0;

	public int MapSRCCount = 0;

	public int PageIndex = 1;

	public int PageSize = 10;

	public int PageCount = 0;

	public Map<String, IHoyiCtrl> childrends = new HashMap();
	public String Pageurl;
	public String Id;
	public String Width;
	public String Height;
	public String BackgroundColor;
	public String FontColor;
	public String FontSize;
	public String Margin;
	public String Padding;
	public String Top;
	public String Left;
	public String Right;
	public String Bottom;
	public String CSSClass;
	public String Style;
	public String Text;
	public String InnerText;
	public String InnerHTML;
	public OnClickListener OnClick;
	

	public void Init() {
	}

	public String Render() {
		return "";
	}

	public String RenderT(String Template) {
		return "";
	}
	
	public IHoyiCtrl(){}

	public IHoyiCtrl(String ctrid) {
		setId(ctrid);
	}

	public IHoyiCtrl(String ctrid, IHoyiCtrl parent) {
		setId(ctrid);
		setParentCtrl(parent);
		parent.childrends.put(getId(), this);
		this.ExecuteListener = parent.ExecuteListener;
		this.setRequest(parent.getRequest());
		this.setResponse(parent.getResponse());
	}

	public IHoyiCtrl getParentCtrl() {
		return this.ParentCtrl;
	}

	public void setParentCtrl(IHoyiCtrl parentCtrl) {
		this.ParentCtrl = parentCtrl;
	}

	public String getId() {
		return this.Id;
	}

	public void setId(String id) {
		this.Id = id;
	}

	public String getHtmlContent() {
		return this.HtmlContent;
	}

	public String getRendedHTMLContent() {
		return this.rendedHTMLContent;
	}

	public void setHtmlContent(String htmlContent) {
		this.HtmlContent = htmlContent;
	}

	public String InitContentByTemplates(String _template) {
		return _template;
	}

	public String TransEXCUTEMethod(String _template) {
		if (this.ExecuteListener != null) {
			//Console.Info("ExecuteListener.Execute");
			return this.ExecuteListener.Execute(_template);
		}
		Console.Info("No ExecuteListener.");

		return _template;
	}

	public void CalcPageCount(int _datacount, int _pagesize) {
		if (_datacount > 0) {
			int pgcount = _datacount / _pagesize;
			if (_datacount % _pagesize > 0) {
				pgcount++;
			}
			setPageCount(pgcount);
			if (getPageIndex() > getPageCount())
				setPageIndex(getPageCount());
		} else {
			setPageCount(0);
		}
	}

	public int getPageCount() {
		return this.PageCount;
	}

	public void setPageCount(int pageCount) {
		this.PageCount = pageCount;
	}

	public int getPageSize() {
		return this.PageSize;
	}

	public void setPageSize(int pageSize) {
		this.PageSize = pageSize;
	}

	public int getPageIndex() {
		return this.PageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.PageIndex = pageIndex;
	}
	
	public void SetParamPageIndex() {
		int pgindx = GetParamsInt("pgindex", 1);
		this.setPageIndex(pgindx);
	}

	public int getDataCount() {
		return this.DataCount;
	}

	public void setDataCount(int dataCount) {
		this.DataCount = dataCount;
		CalcPageCount(DataCount, this.getPageSize());
	}

	public int getMapSRCCount() {
		return this.MapSRCCount;
	}

	public void setMapSRCCount(int mapSRCCount) {
		this.MapSRCCount = mapSRCCount;
	}

	public void DataBind() {
	}

	public void Click() {
		if (this.OnClick != null)
			this.OnClick.Click();
	}
}
